package com.example.designpatterns.behavioral.interpreter;

/**
 * 终结符表达式 - 替换单词
 *
 * @author scott
 */
class ReplaceExpression implements Expression {

    private String word;

    public ReplaceExpression(String word) {
        this.word = word;
    }

    @Override
    public void interpret(Context context) {
        String text = context.getText();
        String replacement = context.getReplacement(word);

        // 在文本中替换指定单词
        String newText = text.replaceAll(word, replacement);
        context.setText(newText);
    }

}
